import React from "react";
import { createSlice,createAsyncThunk } from "@reduxjs/toolkit";
import _ from 'lodash';

export const getList = createAsyncThunk('article/getList',
   async ({currentPage=1,pageSize=5}:{ currentPage?: number, pageSize?: number, type?: string }) => {
        const promise = new Promise((resolve,reject)=>{
            setTimeout(()=>{
                resolve({data:{list:[1,2,3,4,5],total:5}})
            },500)
        })
        try {
            var [res] = await Promise.all([promise]);
        } catch (error) {
            console.error(error);
        }
        const payload = _.get(res, 'data', { list: [], total: 0 });
        console.log(payload);
        return payload;
   }
)

export const article = createSlice({
    name:'article',
    initialState:{
        list:[],
        total:0
    },
    reducers:{},
    extraReducers:(builder)=>{
        builder.addCase(getList.fulfilled,(state,{payload})=>{
            state.list = payload.list
            state.total = payload.total;
        })
    }
})

export const {} = article.actions;
export default article.reducer;